﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;

namespace BskLeague.Controllers
{
    public class RankingController : Controller
    {

        BskLeagueDB db = new BskLeagueDB();

        public ActionResult Index(int season_id = 0)
        {
            Season season = this.SetSeason(season_id);

            List<SeasonTeamPlayer> stp = this.GetStp(ViewBag.Season.SeasonId);

            List<Player> players = new List<Player>();

            foreach (var i in stp)
            {
                players.Add(db.Players.Find(i.Player.PlayerId));
            }

            if (players.Count() > 0)
                ViewBag.IsPlayers = true;
            else
                ViewBag.IsPlayers = false;

            List<PlayersRankingHelper> PlayersList = new List<PlayersRankingHelper>();

            foreach (var p in players)
            {

                PlayersRankingHelper prh = new PlayersRankingHelper();

                var p_list = (from po in db.Points
                              where po.Player.PlayerId == p.PlayerId &&
                              po.Match.Season.SeasonId == season.SeasonId
                              select po.ScoredPoints).ToList();

                prh.Player = p;
                prh.Team = db.Teams.Find(stp.Where(s => s.Player.PlayerId == p.PlayerId).First().Team.TeamId);
                prh.TotalMatches = p_list.Count();
                prh.TotalPoints = p_list.Sum();

                prh.TotalFauls = (from po in db.Fauls
                                 where po.Player.PlayerId == p.PlayerId &&
                                 po.Match.Season.SeasonId == season.SeasonId
                                 select po.FaulsCommited).ToList().Sum();

                prh.GamesMinutes = (from po in db.Minutes
                                    where po.Player.PlayerId == p.PlayerId &&
                                    po.Match.Season.SeasonId == season.SeasonId
                                    select po.MinutesPlayed).ToList().Sum();

                if (prh.TotalMatches > 0)
                {
                    prh.AveragePoints = Math.Round((prh.TotalPoints / prh.TotalMatches), 2);
                    prh.AverageFauls = Math.Round((prh.TotalFauls / prh.TotalMatches), 2);
                }
                else
                {
                    prh.AverageFauls = 0.00;
                    prh.AveragePoints = 0.00;
                }

                PlayersList.Add(prh);
            }

            ViewBag.PlayersList = PlayersList;

            return View();
        }        

        public ActionResult Team(int season_id = 0)
        {
            Season season = this.SetSeason(season_id);

            List<SeasonTeamPlayer> stp = this.GetStp(ViewBag.Season.SeasonId);

            List<Team> teams = new List<Team>();

            foreach (var i in stp)
            {
                teams.Add(db.Teams.Find(i.Team.TeamId));
            }

            if (teams.Count() > 0)
                ViewBag.IsTeams = true;
            else
                ViewBag.IsTeams = false;

            List<Team> teams_l = teams.Distinct().ToList();

            List<TeamsRankingHelper> TeamsList = new List<TeamsRankingHelper>();

            foreach (var t in teams_l)
            {
                TeamsRankingHelper trh = new TeamsRankingHelper();

                trh.Team = t;

                trh.TotalMatches = (from po in db.Matchs
                                 where ((po.Team1.TeamId == t.TeamId) || (po.Team2.TeamId == t.TeamId)) &&
                                 po.Season.SeasonId == season.SeasonId
                                 select po.MatchId).ToList().Count();

                trh.TotalFauls = (from f in db.Fauls
                                join d in db.SeasonTeamPlayers
                                on f.Player.PlayerId equals d.Player.PlayerId
                                where d.Team.TeamId == t.TeamId &&
                                f.Match.Season.SeasonId == season.SeasonId &&
                                d.Season.SeasonId == season.SeasonId
                                select f.FaulsCommited).ToList().Sum();

                int total1 = (from f in db.Matchs
                              where f.Season.SeasonId == season.SeasonId && f.Team1.TeamId == t.TeamId
                              select f.FinalScore1).ToList().Sum();

                int total2 = (from f in db.Matchs
                              where f.Season.SeasonId == season.SeasonId && f.Team2.TeamId == t.TeamId
                              select f.FinalScore2).ToList().Sum();


                trh.TotalPoints = total1 + total2;

                if (trh.TotalMatches > 0)
                {
                    trh.AveragePoints = Math.Round((trh.TotalPoints / trh.TotalMatches), 2);
                    trh.AverageFauls = Math.Round((trh.TotalFauls / trh.TotalMatches), 2);
                }
                else
                {
                    trh.AverageFauls = 0.00;
                    trh.AveragePoints = 0.00;
                }

                TeamsList.Add(trh);

            }

            ViewBag.TeamsList = TeamsList;

            return View();
        }

        private Season SetSeason(int season_id = 0)
        {
            Season season;

            var seasons_list = (from m in db.Seasons
                                where m.StatusId == 2 || m.StatusId == 3
                                select m).ToList();

            if (season_id == 0)
            {

                season = seasons_list.First();
            }
            else
            {
                season = db.Seasons.Find(season_id);
            }

            ViewBag.Season = season;
            ViewBag.SeasonsList = seasons_list;

            return season;
        }

        private List<SeasonTeamPlayer> GetStp(int season_id)
        {
            List<SeasonTeamPlayer> stp = (from m in db.SeasonTeamPlayers
                                          where m.Season.SeasonId == season_id
                                          select m).ToList();

            return stp;
        }

    }
}

public class PlayersRankingHelper
{
    public Player Player        { get; set; }
    public Team Team            { get; set; }
    public double TotalMatches     { get; set; }
    public int TotalPoints      { get; set; }
    public double AveragePoints { get; set; }
    public int TotalFauls       { get; set; }
    public double AverageFauls  { get; set; }
    public int GamesMinutes     { get; set; }
}

public class TeamsRankingHelper
{
    public Team Team { get; set; }
    public double TotalMatches { get; set; }
    public int TotalPoints { get; set; }
    public double AveragePoints { get; set; }
    public int TotalFauls { get; set; }
    public double AverageFauls { get; set; }
}